<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/4
 * Time: 16:23
 */

namespace app\admin\controller;


use app\admin\model\CategoryList;
use app\admin\model\LunboList;
use think\Controller;

class Lunbo extends Controller
{
    public function lists(){
        return $this->fetch();
    }

    public function data($page,$limit){
        $where = '';
        $data = LunboList::where($where)->order('sort desc')->page($page,$limit)->select()->toArray();
        foreach ($data as $key=>$v){
            $category=CategoryList::where('id',$v['category_id'])->find();
            $data[$key]['category_id']=$category['title'];
        }
        $count = LunboList::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function add(){
        if ($this->request->isPost()){
            $post = input('post.');
            $data = [
                'image'=>$post['image'],
                'url'=>$post['url'],
                'category_id'=>$post['category_id'],
                'sort'=>$post['sort']
            ];
            $ret = LunboList::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        //查询分类信息
        $categoryList=CategoryList::select();
        $this->assign('categoryList',$categoryList);
        return $this->fetch();
    }

    public function edit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            $data = [
                'image'=>$post['image'],
                'url'=>$post['url'],
                'category_id'=>$post['category_id'],
                'sort'=>$post['sort']
            ];
            $ret = LunboList::update($data,['id'=>$post['id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = LunboList::where('id',$id)->find();
        $this->assign('data',$data);

        //查询分类信息
        $category=CategoryList::where('id',$data['category_id'])->find();
        $this->assign('category',$category);

        //查询分类信息
        $categoryList=CategoryList::select();
        $this->assign('categoryList',$categoryList);
        return $this->fetch();
    }

    public function del(){
        $id = input('post.id');
        $ret = LunboList::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }

    public function LunboImage(){
        $file =request()->file('file');
        $info = $file->move( '../public/uploads');
        if ($info) {
            $result = [
                'code'     => 1,
                'msg'      => '上传成功',
                'url' => '/uploads/' . str_replace('\\', '/', $info->getSaveName())
            ];
        } else {
            $result = [
                'code' => 0,
                'msg'  => $file->getError()
            ];
        }
        return $result;
    }

}